/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import conexao.Conexao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import modelo.Fabricante;

public class FabricanteDAO {
    
    private Connection conexao;
    private PreparedStatement stmt;
    private ResultSet rs;
    private String sql;

    public FabricanteDAO() throws Exception {
        try {
            this.conexao = Conexao.getConexao();
        } catch (Exception ex) {
            throw new Exception("Erro: " + ex.getMessage());
        }
    }

    public void finalizar() throws Exception {
        try {
            Conexao.closeConexao(this.conexao, this.stmt, this.rs);
        } catch (SQLException ex) {
            throw new Exception("Erro ao finalizar " + ex);
        }
    }
    
    public void incluir(Fabricante fabricante) throws Exception {
        try {
            sql = "insert into FABRICANTE (ID, NOME) values (?, ?)";
            stmt = conexao.prepareStatement(sql);
            stmt.setInt(1, fabricante.getId());
            stmt.setString(2, fabricante.getNome());            
            stmt.execute();
            stmt.close();
        } catch (SQLException ex) {
            throw new Exception("Erro ao incluir " + ex);
        }
    }

    public void alterar(Fabricante fabricante) throws Exception {
        try {
            sql = "update FABRICANTE set NOME=? where ID=?";
            stmt = conexao.prepareStatement(sql);
            stmt.setString(1, fabricante.getNome());
            stmt.setInt(2, fabricante.getId());
            stmt.execute();
            stmt.close();
        } catch (SQLException ex) {
            throw new Exception("Erro ao alterar " + ex);
        }
    }

    public void excluir(Integer id) throws Exception {
        try {
            sql = "delete from FABRICANTE where ID=?";
            stmt = conexao.prepareStatement(sql);
            stmt.setInt(1, id);
            stmt.execute();
            stmt.close();
        } catch (SQLException ex) {
            throw new Exception("Erro ao excluir " + ex);
        }
    }

    public Fabricante consultar(Integer id) throws Exception {
        try {
            sql = "select * from FABRICANTE where ID=?";
            stmt = conexao.prepareStatement(sql);
            stmt.setInt(1, id);
            rs = stmt.executeQuery();
            Fabricante fabricante = new Fabricante();
            if (rs.next()) {
                fabricante.setId(rs.getInt("id"));
                fabricante.setNome(rs.getString("nome"));
            }
            rs.close();
            stmt.close();
            return fabricante;
        } catch (SQLException ex) {
            throw new Exception("Erro ao consultar " + ex);
        }    
    }
    
    public List<Fabricante> listar() throws Exception {
        try {
            sql = "select * from FABRICANTE order by NOME";
            stmt = conexao.prepareStatement(sql);
            rs = stmt.executeQuery();
            List<Fabricante> fabricantes = new ArrayList<Fabricante>();
            
            while (rs.next()) {
                Fabricante fabricante = new Fabricante();
                fabricante.setId(rs.getInt("id"));
                fabricante.setNome(rs.getString("nome"));
                
                fabricantes.add(fabricante);
            }
            rs.close();
            stmt.close();
            return fabricantes;
        } catch (SQLException ex) {
            throw new Exception("Erro ao listar " + ex);
        }
    }    
}
